'''
Created on Aug 13, 2012

@author: petrina
'''
import logging
from record_list import Record_list

class Link(object):
    '''
    This class includes static Link-data such as length, name and frc.
    
    Links are part of the street graph. FLEET-data is based on Links.
    '''


    def __init__(self,id = -1, direction = True, length = 0, name = '', frc = -1, 
                 freespeed = 0, is_kph = True, intervals = 96):
        '''
        
        @param id: linkid (default: -1)
        @param direction: direction of the link (default: True, i.e. FT/From-To)
        @param length: link length in meters (default: 0)
        @param name: link's name 
        @param frc: functional road class (default: -1)
        @param freespeed: free flow speed (default: 0) 
        '''

        self.id = id
        self.direction = direction
        self.length = length
        self.name = name
        self.frc = frc
        self.freespeed = freespeed
        self.is_kph = is_kph
        
        # data of the link
        self.records = Record_list(name = 'agg_fleet_records', intervals = intervals)
        self.reference = Record_list(name ='reference_data', intervals = intervals)
        self.timeseries = Record_list(name='fleet_timeseries', intervals = intervals)
        
        logging.debug("Link: created new Link " + self.__str__())
        
        self._linkname = str(self.__id) + "("+ str(self.__direction) + ")"
        self._exportname = str(self.__id) + "_"+ str(self.__direction)


    #-----------------------getter- and setter-methods-------------------------

    
    def get_linkname(self):
        return self._linkname


    def get_exportname(self):
        return self._exportname

        
    def get_is_kph(self):
        return self.__is_kph


    def set_is_kph(self, value):
        self.__is_kph = value


    def del_is_kph(self):
        del self.__is_kph    
    
    def get_timeseries(self):
        return self.__timeseries


    def set_timeseries(self, value):
        self.__timeseries = value


    def del_timeseries(self):
        del self.__timeseries

    
    def get_reference(self):
        return self.__reference


    def set_reference(self, value):
        self.__reference = value


    def del_reference(self):
        del self.__reference

    
    def get_records(self):
        return self.__records


    def set_records(self, value):
        self.__records = value


    def del_records(self):
        del self.__records


    def get_freett(self):      
        if self.__freespeed is not None and self.__freespeed > 0:
            # * 1000 ==> meters, *60*60 ==> seconds 
            return 3.6 * self.__length / self.__freespeed if self.__is_kph else self.__length / self.__freespeed          
        else:
            logging.error("Link: cannot calculate freeflow travel time because the freespeed is invalid " + self.__freespeed)
            return None


    def get_freespeed(self):
        return self.__freespeed


    def set_freespeed(self, value):
        self.__freespeed = value


    def del_freespeed(self):
        del self.__freespeed

                
    def get_id(self):
        return self.__id


    def get_direction(self):
        return self.__direction


    def get_length(self):
        return self.__length


    def get_name(self):
        return self.__name


    def get_frc(self):
        return self.__frc


    def set_id(self, value):
        self.__id = value


    def set_direction(self, value):
        self.__direction = value


    def set_length(self, value):
        self.__length = value


    def set_name(self, value):
        self.__name = value


    def set_frc(self, value):
        self.__frc = value


    def del_id(self):
        del self.__id


    def del_direction(self):
        del self.__direction


    def del_length(self):
        del self.__length


    def del_name(self):
        del self.__name


    def del_frc(self):
        del self.__frc
        
    def __str__(self):
        return str(self.__id) + ("FT" if self.__direction else "TF") + " l="+str(self.__length) + " m"
    
    
    def plot(self, plotobject):
        '''
        plots the data (reference, records and timeseries) of the link
        
        @param link: link for plotting data 
        '''
        
        linkname = str(self.__id) + "("+ str(self.__direction) + ")"
        exportname = str(self.__id) + "_"+ str(self.__direction)
        
        self.__reference.plot(plotobject, name = "reference " + linkname, exp_name = "ref_" + exportname)
        self.__timeseries.plot(plotobject, name = "timeseries " + linkname, exp_name =  "ts_" + exportname)
        self.__records.plot(plotobject, name = "records " + linkname, exp_name = "rec_" + exportname)
   
    
    #--------------assign properties using get and set-methods-----------------

    # static properties from the db
    id = property(get_id, set_id, del_id, "(numeric) id of the link (e.g. Tele Atlas link id)")
    direction = property(get_direction, set_direction, del_direction, "direction of the link - True = FT/From-To, False = TF/To-From")
    length = property(get_length, set_length, del_length, "length of the link in meters")
    name = property(get_name, set_name, del_name, "name or description of the link (e.g. Giefinggasse)")
    frc = property(get_frc, set_frc, del_frc, "frc = functional road class (see Tele Atlas documentation for more information)")    
    freespeed = property(get_freespeed, set_freespeed, del_freespeed, "free-flow speed at this link")
    freett = property(get_freett, None, None, "free flow travel time")
    
    # aggregated data
    records = property(get_records, set_records, del_records, "regular fleet data for this link (as Record_list-object)")
    reference = property(get_reference, set_reference, del_reference, "reference data for this link (as Record_list-object)")
    timeseries = property(get_timeseries, set_timeseries, del_timeseries, "timeseries containing std for one day (as Record_list-object)")
    is_kph = property(get_is_kph, set_is_kph, del_is_kph, "indicates whether speeds are in km/h (true) or m/s (false)")
    linkname = property(get_linkname, None, None, "name of the link (for displaying)")
    exportname = property(get_exportname, None, None, "machine readable name of the link for the file export")
